package me.lc.rank;

import java.util.ArrayList;
import java.util.List;

import me.lc.FunPeaceActivity;
import me.lc.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RankActivity extends Activity {

	static TextView txtMyRank;
	static TextView txtMyScore;
	static ListView lstTop10;
	static Button btnReturn;
	public static List<RankInfo> top10Info;
	static RelativeLayout maskLayout;
	static RankListAdapter da;
	static RankActivity instance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.top_rank_view);
		instance = this;
		setUI();
	}

	private void setUI() {
		txtMyRank = (TextView) findViewById(R.id.txtMyRank);
		txtMyScore = (TextView) findViewById(R.id.txtMyScore);
		lstTop10 = (ListView) findViewById(R.id.listViewTop10);
		maskLayout = (RelativeLayout) findViewById(R.id.layoutRankLoading);
		btnReturn = (Button) findViewById(R.id.btnReturn);

		// 显示我的成绩
		txtMyScore.setText(FunPeaceActivity.Instance.piCount + " 个屁");

		SAEHelper.submitScore(FunPeaceActivity.Instance.rankID, "piUser", 1,
				FunPeaceActivity.Instance.piCount);

		// 设置
		top10Info = new ArrayList<RankInfo>();
		da = new RankListAdapter();
		lstTop10.setAdapter(da);

		// // 载入数据
		// SAEHelper.getMyRank(1);

		btnReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					RankActivity.instance.finish();
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static Handler loadCompleteHadler = new Handler() {
		@Override
		public void handleMessage(final android.os.Message msg) {
			// 1-用户排名载入完毕 2-Top10载入完毕
			RankActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (msg.what == 1) {
						txtMyRank.setText("第" + msg.arg1 + "名");
					} else if (msg.what == 2) {
						maskLayout.setVisibility(View.INVISIBLE);
						da.notifyDataSetChanged();
					}
				}
			});

		};
	};

	class RankListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return top10Info.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			RankInfo info = top10Info.get(position);

			View item = RankActivity.this.getLayoutInflater().inflate(
					R.layout.top10_item_view, null);
			TextView txtRank = (TextView) item.findViewById(R.id.txtTopRank);
			TextView txtScore = (TextView) item.findViewById(R.id.txtTopScore);

			txtRank.setText("第" + info.getNo() + "名");
			txtScore.setText(info.getScore() + " 个屁");

			if (info.isSelf()) {
				txtRank.setTextColor(Color.RED);
				txtScore.setTextColor(Color.RED);
			}

			return item;
		}
	}

}
